#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayVrrpVerbose < Aio::Module::Cmd::H3C
  include Aio::Module

  def initialize
    super({
      :cmd_full			=> "display vrrp verbose",
      :cmd_short		=> "dis vrrp ver",
      :author				=> "Elin",
      :description  => "This is H3C Command# display vrrp verbose",
      :ranking			=> Ranking_1,
      :platform			=> "all",
      :benchmark		=> {},
    })
  end

  def parse
    cont = self.context.dup
    vrrp = {}
    useful[:vrrp] = vrrp
    cont.readline_match_block(/Run Mode : (?<run_mode>.*)/) do |block|
      block.update(vrrp, :run_mode)
    end
    cont.readline_match_block(/Run Method : (?<run_method>.*)/) do |block|
      block.update(vrrp, :run_method)
    end
    cont.readline_match_block(/Total number of virtual routers : (?<total_number_vrrp>\d+)/) do |block|
      block.update(vrrp, :total_number_vrrp)
    end
    cont.readline_range(/Interface/, /Interface/) do |cont_layer|
      parse_vrrp(cont_layer)
    end
  end

  def parse_vrrp(cont)
    vrrp = {}
    useful[:vrrp][:vrid] = {}
    iface_id = nil

    case device_template
    when Template_2
      cont.readline_match_block(/Interface : (?<iface_id>.[^\s]+)/) do |block|
        iface_id = block[:iface_id]
      end
    else
      cont.readline_match_block(/Interface (?<iface_id>.*)/) do |block|
        iface_id = block[:iface_id]
      end
    end

    cont.readline_match_block(/VRID : (?<vrid>\d+)/) do |block|
      vrid = block[:vrid]
      useful[:vrrp][:vrid][vrid] = vrrp
      block.update(vrrp, :interface, iface_id)
      block.update(vrrp, :vrid)
    end
    cont.readline_match_block(/Admin Status : (?<status>.*) State : (?<state>.*)/) do |block|
      block.update(vrrp, :status)
      block.update(vrrp, :state)
    end
    cont.readline_match_block(/Config Pri : (?<config_pri>.*) Running Pri : (?<running_pri>.*)/) do |block|
      block.update(vrrp, :config_pri)
      block.update(vrrp, :running_pri)
    end
    cont.readline_match_block(/Preempt Mode : (?<preempt_mode>.*) Delay Time : (?<delay_time>\d+)/) do |block|
      block.update(vrrp, :preempt_mode)
      block.update(vrrp, :delay_time)
    end
    cont.readline_match_block(/Auth Type : (?<auth_type>.*)/) do |block|
      block.update(vrrp, :auth_type)
    end
    cont.readline_match_block(/Virtual IP : (?<virtual_ip>.*)/) do |block|
      block.update(vrrp, :virtual_ip)
    end
    cont.readline_match_block(/Virtual MAC : (?<virtual_mac>.*)/) do |block|
      block.update(vrrp, :virtual_mac)
    end
    cont.readline_match_block(/Master IP : (?<master_ip>.*)/) do |block|
      block.update(vrrp, :master_ip)
    end
  end
end

